﻿@using Entidades.Sistema.Base;
@using Entidades.Sistema.Produccion;
@using System.Collections.ObjectModel;


@{
    Html.EnableClientValidation(false);
    Html.EnableUnobtrusiveJavaScript(false);

    var grid = Html.DevExpress().GridView(
        settings =>
        {
            settings.Name = "gvEditing";
            settings.KeyFieldName = "IdDiagramacionProduccion";
            settings.CallbackRouteValues = new { Controller = "DiagramacionProduccion", Action = "EditDiagramacionProduccionPartial" };

            settings.Columns.Add(column =>
            {
                column.Caption = "#";
                column.SetDataItemTemplateContent(c =>
                {
                    var estado = DataBinder.Eval(c.DataItem, "Estado").ToString();

                    //TODO Crear propiedad PermiteEdit en la entidad
                    if (estado == "Creada" || estado == "Editada")
                    {
                        if (ViewBag.PermisoEdit)
                        {
                            ViewContext.Writer.Write(

                            Html.ActionLink("Editar", "EditDiagramacionProduccionUpdatePartial", new { IdDiagramacionProduccion = DataBinder.Eval(c.DataItem, "IdDiagramacionProduccion") }) + "&nbsp;"
                            );
                        }
                    }
                    if (ViewBag.PermisoEdit)
                    {
                        ViewContext.Writer.Write(

                        Html.ActionLink("CambiarEstado", "CambiaEstadoDiagramacionProduccionPartial", new { IdDiagramacionProduccion = DataBinder.Eval(c.DataItem, "IdDiagramacionProduccion") }) + "&nbsp;"
                        );
                    }                                       

                });
                column.SetHeaderTemplateContent(c =>
                {
                    if (ViewBag.PermisoCreate)
                    {
                        ViewContext.Writer.Write(
                        Html.ActionLink("Nuevo", "EditDiagramacionProduccionUpdatePartial", new { IdDiagramacionProduccion = -1 }).ToHtmlString());                    
                    }                    
                });
                column.Settings.AllowDragDrop = DefaultBoolean.False;
                column.Settings.AllowSort = DefaultBoolean.False;
                column.Width = 70;
            });

            settings.Columns.Add(columnImprimir =>
            {
                columnImprimir.SetDataItemTemplateContent(c =>
                {
                    var estado = DataBinder.Eval(c.DataItem, "Estado").ToString();
                    
                    if (estado != "Cancelada")
                    {
                        ViewContext.Writer.Write(
                        Html.ActionLink("Imprimir", "ImprimirDiagramacion", new { IdDiagramacionProduccion = DataBinder.Eval(c.DataItem, "IdDiagramacionProduccion") },
                                new { onclick = "return confirm('Realmente quiere imprimir la diagramación?')" }));
                    }
                });
                columnImprimir.Settings.AllowDragDrop = DefaultBoolean.False;
                columnImprimir.Settings.AllowSort = DefaultBoolean.False;
            });
            
            settings.SettingsBehavior.ConfirmDelete = true;
            settings.SettingsPopup.EditForm.Width = 800;
            settings.SettingsPopup.EditForm.HorizontalAlign = PopupHorizontalAlign.Center;
            settings.SettingsPopup.EditForm.VerticalAlign = PopupVerticalAlign.WindowCenter;
            settings.SettingsPager.FirstPageButton.Visible = true;
            settings.SettingsPager.LastPageButton.Visible = true;
            settings.SettingsPager.PageSizeItemSettings.Visible = true;
            settings.SettingsPager.PageSizeItemSettings.Items = new string[] { "10", "20", "50" };
            
            
            settings.Settings.ShowFilterRow = true;
            settings.Settings.ShowFilterRowMenu = true;
            settings.CommandColumn.Visible = true;
            settings.CommandColumn.ShowClearFilterButton = true;
            
            settings.Width = System.Web.UI.WebControls.Unit.Percentage(100);

            settings.Columns.Add("IdDiagramacionProduccion");
            settings.Columns.Add(column =>
            {
                column.FieldName = "Fecha";
                column.Caption = "Fecha";
                column.ColumnType = MVCxGridViewColumnType.DateEdit;
                column.PropertiesEdit.DisplayFormatString = "dd/MM/yyyy";
            });
            settings.Columns.Add("Estado");           

            settings.PreRender = (s, e) =>
            {
                var sender = (MVCxGridView)s;
                sender.FocusedRowIndex = 0;
            };
            
            settings.SettingsBehavior.AllowFocusedRow = true;
        });

    if (ViewData["EditError"] != null){
        grid.SetEditErrorText((string)ViewData["EditError"]);
    }
}

@grid.Bind(Model).GetHtml()









